package com.swjd.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import java.util.concurrent.Future;

/**
 * @author liutao
 */
@Service
public class MyAsyncService {
    /**
     * 标明异步方法
     */
    @Async
    public void sendSMS() throws InterruptedException {
        System.out.println("调用短信验证码业务方法.....");
        Long startTime = System.currentTimeMillis();
        //模拟业务逻辑处理时间
        Thread.sleep(5000);
        Long endTime = System.currentTimeMillis();
        System.out.println("短信业务完成耗时：" + (endTime - startTime));
    }

    @Async
    public Future<Integer> processA() throws InterruptedException {
        System.out.println("开始分析并统计业务A数据...");
        long startTime = System.currentTimeMillis();
        Thread.sleep(4000);
        //模拟统计结果
        int count = 123456;
        long endTime = System.currentTimeMillis();
        System.out.println("业务A数据统计耗时：" + (endTime - startTime));
        return new AsyncResult<Integer>(count);
    }

    @Async
    public Future<Integer> processB() throws InterruptedException {
        System.out.println("开始分析并统计业务B数据...");
        long startTime = System.currentTimeMillis();
        Thread.sleep(5000);
        //模拟统计结果
        int count = 654321;
        long endTime = System.currentTimeMillis();
        System.out.println("业务B数据统计耗时：" + (endTime - startTime));
        return new AsyncResult<Integer>(count);
    }

}
